package com.gulimall.order.config;


import com.gulimall.common.constrant.SeckillConstant;
import com.gulimall.common.constrant.WareConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.gulimall.common.constrant.OrderConstant.*;

/**
 * @author kkk
 * @date 2022/8/6
 */
@Configuration
public class RabbitExchangeQueueConfig {
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(ORDER_EVENT_EXCHANGE, true, false);
    }

    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> param = new HashMap<>();
        param.put("x-dead-letter-exchange", ORDER_EVENT_EXCHANGE);
        param.put("x-message-ttl", ttlTime);
        param.put("x-dead-letter-routing-key", RELEASE_ORDER_KEY);
        return new Queue(ORDER_DELAY_QUEUE, true, false, false, param);
    }

    @Bean
    public Queue releaseOrderQueue() {
        return new Queue(ORDER_RELEASE_QUEUE, true, false, false);
    }

    @Bean
    public Binding createOrderBinding() {
        return new Binding(ORDER_DELAY_QUEUE, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE, CREATE_ORDER_KEY,
                null);
    }

    @Bean
    public Binding releaseOrderBinding() {
        return new Binding(ORDER_RELEASE_QUEUE, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE,
                RELEASE_ORDER_KEY, null);
    }

    @Bean
    public Binding stockReleaseBinding() {
        //将库存队列绑定到订单交换机上， 当订单取消后将消息直接发到库存解锁队列，直接解锁库存
        return new Binding(WareConstant.WARE_STOCK_RELEASE_QUEUE, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE
                , ORDER_BINDING_STOCK_RELEASE_KEY, null);
    }

    @Bean
    public Queue seckillOrderQueue() {
        return new Queue(SeckillConstant.SECKILL_ORDER_QUEUE, true, false, false);
    }

    @Bean
    public Binding seckillBinding() {
        return new Binding(SeckillConstant.SECKILL_ORDER_QUEUE, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE,
                SeckillConstant.SECKILL_ORDER_KEY, null);
    }
}
